package com.th.coupon.entity;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.th.coupon.common.constant.CouponCategory;
import com.th.coupon.common.constant.DistributeTarget;
import com.th.coupon.common.constant.ProductLine;
import com.th.coupon.common.vo.TemplateRule;
import com.th.coupon.converter.CouponCategoryConverter;
import com.th.coupon.converter.DistributeTargetConverter;
import com.th.coupon.converter.ProductLineConverter;
import com.th.coupon.converter.TemplateRuleConverter;
import com.th.coupon.seralization.CouponTemplateSerialize;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 优惠卷模板实体类
 * {@Transient 忽略实体与数据表的映射
 * @Basic 基本映射（默认）
 * @JsonSerialize 对象自定义序列化器
 * @Table 实体对应数据库表名
 * @EntityListeners 通过该监听器自动的生成一些值 比如@CreatedDate
 * @Convert 属性转换器
 * }
 * @author 涂航
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "coupon_template")
@JsonSerialize(using= CouponTemplateSerialize.class)
public class CouponTemplate implements Serializable {

    /**
     * 设置主键
     * 主键自增
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id",nullable = false)
    private Integer id;
    /**
     * 是否可用状态
     */
    @Column(name = "available",nullable = false)
    private Boolean available;
    /**
     * 是否过期
     */
    @Column(name = "expired",nullable = false)
    private Boolean expired;

    /**
     * 优惠卷名称
     */
    @Column(name = "name",nullable = false)
    private String name;
    /**
     * 优惠卷logo
     */
    @Column(name = "logo",nullable = false)
    private String logo;

    /**
     * 优惠卷描述
     */
    @Column(name = "intro",nullable = false)
    private String desc;

    /**
     * 优惠卷分类
     */
    @Column(name = "category",nullable = false)
    @Convert(converter = CouponCategoryConverter.class)
    private CouponCategory category;

    /**
     * 产品线
     */
    @Column(name = "product_line",nullable = false)
    @Convert(converter = ProductLineConverter.class)
    private ProductLine productLine;
    /**
     * 总数
     */
    @Column(name = "coupon_count",nullable = false)
    private Integer count;

    /**
     * 创建时间  @UpdateTimestamp @CreatedBy
     *
     */
    @CreatedDate
    @Column(name = "create_time",nullable = false)
    private Date createTime;

    /**
     * 创建用户
     */
    @Column(name = "user_id",nullable = false)
    private Long userId;

    /**
     * 优惠卷编码
     */
    @Column(name = "template_key",nullable = false)
    private String key;

    /**
     * 目标用户
     */
    @Column(name = "target",nullable = false)
    @Convert(converter = DistributeTargetConverter.class)
    private DistributeTarget target;

    /**
     * 优惠卷规则
     */
    @Column(name = "rule",nullable = false)
    @Convert(converter = TemplateRuleConverter.class)
    private TemplateRule rule;


    public CouponTemplate(String name,String logo,String desc,
    String category,Integer productLine,Integer count,Long userId,
                          Integer target,TemplateRule rule){
        this.available = false;
        this.expired = false;
        this.name = name;
        this.desc = desc;
        this.logo = logo;
        this.category = CouponCategory.of(category);
        this.productLine = ProductLine.of(productLine);
        this.count = count;
        this.userId = userId;
        this.target = DistributeTarget.of(target);
        this.rule = rule;
        //优惠卷唯一编码  4位（的产品和类型）+8位（日期）+id（扩充为4位）
        this.key = productLine.toString() + category +
                new SimpleDateFormat("yyyyMMdd").format(new Date());



    }

}
